The solution is to serialize head_dtype in `ImageClassifier.get_config()#2614
The solution is to serialize head_dtype in `ImageClassifier.get_config()#2614yashwanth510 wants to merge 4 commits intokeras-team:masterfrom
head_dtype in `ImageClassifier.get_config()#2614Conversation
head_dtype was accepted by __init__() and used to set the dtype policy for pooler, output_dropout, and output_dense layers, but was never stored on self or included in get_config(). This caused head_dtype to be silently lost on save/load round-trips. Fixes keras-team#2085
head_dtype was accepted by __init__() and used to set dtype policy for pooler, output_dropout, and output_dense layers, but was never stored on self or included in get_config(). This caused head_dtype to be silently lost on save/load round-trips. Fixes keras-team#2085
Summary of ChangesHello @yashwanth510, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical issue where the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a serialization issue with head_dtype in the ImageClassifier by ensuring it's stored as an instance attribute and included in the model's configuration. The fix is well-implemented. My review includes minor suggestions to remove temporary comments added for the pull request's context, which will improve long-term code maintainability. While not part of the code changes, I'd also recommend adding an automated test case for model saving to verify this fix and prevent future regressions, as demonstrated in the PR description.
|
I have signed the CLA. |
|
All of the local tests pass, including "test_head_dtype": pytest keras_hub/src/models/resnet/ -v → 9 passed, 9 skipped ✅ The CI failures seem to be caused by tests that were already flaky and have nothing to do with this change (JAX GPU, nightly builds, etc.). cc: @mattdangerw |
head_dtype was accepted by __init__() and used to set dtype policy for classifier head layers, but was never stored on self or included in get_config() in several ImageClassifier subclasses. Affected models: - VitImageClassifier - DeiTImageClassifier - VGGImageClassifier - MobileNetImageClassifier - MobileNetV5ImageClassifier - HGNetV2ImageClassifier This is a follow-up to keras-team#2614 which fixed the same issue in the base ImageClassifier class.
|
Closing in favour of #2617 which includes this fix |
Fix: Preserve
head_dtypeAcross Save/LoadWhat’s the issue?
ImageClassifier.__init__()accepts ahead_dtypeargument and uses it to configure the dtype policy of the classifier head layers:self.poolerself.output_dropoutself.output_denseHowever,
head_dtypewas never stored as an instance attribute and wasn’t included inget_config(). As a result, the value was silently dropped during model serialization.This means that after saving and loading a model, the custom
head_dtypewould no longer be preserved.Root Cause
In
__init__():The value was used to configure layers, but:
self.head_dtypeget_config()So it never made it into the model config.
You can reproduce the issue like this:
The Fix
Two small changes in
image_classifier.py:1. Store the value in
__init__()2. Include it in
get_config()Verification
Expected behavior:
head_dtypeappears in configImpact
This affects all classifiers inheriting from
ImageClassifier, including:ResNetImageClassifierEfficientNetImageClassifierViTImageClassifierFixes #2085.